import { RootState } from '@/types/store'
import { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

export const useInitialState = <StateName extends keyof RootState>(
  action: () => void,
  stateName: StateName
) => {
  // 创建dispatch
  const dispatch = useDispatch()

  // 请求数据
  useEffect(() => {
    // 分发action
    dispatch(action())
  }, [dispatch])

  return useSelector((state: RootState) => state[stateName])
}
